package com.etooling.framework.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.etooling.framework.entity.Employee;
import com.etooling.framework.entity.Org;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {
    /*过期时间 15分钟 */
    private static final long EXPIRE_TIME = 30 * 60 * 1000;
    /*token私钥*/
    private static final String TOKEN_SECRET = "aM*mgUxwUnAPjbpF#Lysoq2Xh@!$JTK2";

    public static String setSession(String token, String key, String value) {

        try {
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            //设置私钥和加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            //设置头部信息
            Map<String, Object> header = new HashMap<>(2);
            header.put("typ", "JWT");
            header.put("alg", "HS256");
            if (WebUtils.checkStr(token)) {
                DecodedJWT jwt = JWT.decode(token);
                Map<String, Claim> values = jwt.getClaims();

                /**
                 * 设置附带信息
                 * 1 头部信息
                 * 23 用户信息
                 * 4 时间
                 * 5 加密算法*/
                JWTCreator.Builder builder = JWT.create()
                        .withHeader(header);
                for (String valueKey : values.keySet()) {
                    builder.withClaim(valueKey, values.get(valueKey).asString());
                }
                builder.withClaim(key, value);
                return builder
                        .withExpiresAt(date)
                        .sign(algorithm);

            } else {
                /**
                 * 设置附带信息
                 * 1 头部信息
                 * 23 用户信息
                 * 4 时间
                 * 5 加密算法*/
                JWTCreator.Builder builder = JWT.create()
                        .withHeader(header);
                return builder
                        .withClaim(key, value)
                        .withExpiresAt(date)
                        .sign(algorithm);
            }
        } catch (Exception e) {
            return null;
        }
    }

    public static boolean verify(String token) {
        try {
            //加密方式
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            //生成jwt规则
            JWTVerifier verifier = JWT.require(algorithm).build();
            //验证token是否符合规则
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String getTokenInfo(String token, String info) {
        try {
            DecodedJWT jwt = JWT.decode(token);
//            Map map=jwt.getClaims();
            return jwt.getClaim(info).asString();
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        Employee e = new Employee();
        e.setName("超级管理员");
        e.setLoginId("admin");
        e.setPwd("apdsfd");
        Org org = new Org();
        org.setName("易图灵");
        String token = JwtUtil.setSession("", "loginUser", e.getLoginId());
        token = JwtUtil.setSession(token, "org", org.getId());
        System.out.println(token);
        System.out.println(JwtUtil.getTokenInfo(token, "loginUser"));
//        System.out.println(JwtUtil.getTokenInfo(token, "org"));
    }

}